import Sprite from "../../js/base/sprite";


export default class Button extends Sprite{

    // conf:{
    //  width:,
    //  height:,
    //  img:,
    //  text:,
    // }
    constructor(ctx,conf) {
        super(conf.img,conf.width,conf.height,conf.x,conf.y);
        this.text = conf.text || "button";
    }

    update(){
        // 判断点击事件

    }

    render(ctx){
        this.drawToCanvas(ctx);

        // 绘制文字
        ctx.fillStyle = '#ffffff';
        ctx.font = '20px Arial';
        let size = Button.getTextSize(ctx,this.text);
        let x = this.x + (this.width - size.width)/2;
        let y = this.y + (this.height + size.height)/2;
        ctx.fillText(this.text,x,y);
    }

    containPoint(x,y){
        if (x >= this.x && x<= this.x + this.width &&
            y >= this.y && y <= this.y + this.height){
            return true;
        }
        return false;
    }

   static getTextSize(ctx,text){
        let info = ctx.measureText(text);
       const textHeight = info.actualBoundingBoxAscent + info.actualBoundingBoxDescent;
        return{
            width:info.width,
            height:textHeight
        }
    }


}